package com.validate.validator;

import com.validate.annotation.MyValidate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MyValidateValidator implements ConstraintValidator<MyValidate, String> {
    @Override
    public void initialize(MyValidate constraintAnnotation) {
        System.out.println("**********initialize");
        System.out.println(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value != null && value.contains(" ")) {
//            //获取默认提示信息
//            String defaultConstraintMessageTemplate = context.getDefaultConstraintMessageTemplate();
//            //禁用默认提示信息
//            context.disableDefaultConstraintViolation();
//            //设置提示语
//            context.buildConstraintViolationWithTemplate(defaultConstraintMessageTemplate).addConstraintViolation();
            System.out.println("********my validator at: " + value);
            return false;
        }
        return true;
    }
}
